﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JKF.Entities.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using JKF.DataModule.SqlServerDAL;
using JKF.Substructure;
using JKF.BusinessLayer.BLL;


namespace JKF.BusinessLayer.BLL
{
    public partial class bllSYS_PARAM_VALUE
    {
        //bllSYS_PARAM_VALUE op_PARAM_VALUE = new bllSYS_PARAM_VALUE();
        dalSYS_PARAM_VALUE partialDal = new dalSYS_PARAM_VALUE();
        dalSYS_PARAM dalSysParam = new dalSYS_PARAM();
        public void Updata(List<SYS_PARAM_VALUEinfo> sysParamValues)
        {
            List<DbCommand> cmds = new List<DbCommand>();
            foreach (SYS_PARAM_VALUEinfo para in sysParamValues)
            {
                if (Exists(para.PARAM_KEY.ToString(), para.UNIT_KEY, para.DEPT_KEY))
                {
                    //修改
                    SYS_PARAM_VALUEinfo temp = GetModel(para.PARAM_KEY.ToString(), para.UNIT_KEY, para.DEPT_KEY);
                    para.ID = temp.ID;
                    cmds.Add(GetUpdateDbCommand(para));
                   
                }
                else
                {
                    //添加

                    para.ID =new Guid(DB.GetNewGuid(DB.HR));
                    cmds.Add(GetAddDbCommand(para));
                }
            }
            ExecTransaction.Exec(DB.HR, cmds);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string PARAM_KEY, string UNIT_KEY, string DEPT_KEY)
        {
            Database db = DB.HR;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SYS_PARAM_VALUE where PARAM_KEY=@PARAM_KEY  ");

            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "@PARAM_KEY", DbType.String, PARAM_KEY);
            

            if (!string.IsNullOrEmpty(UNIT_KEY))
            {
                db.AddInParameter(dbCommand, "@UNIT_KEY", DbType.AnsiString, UNIT_KEY);
                strSql.Append("and UNIT_KEY=@UNIT_KEY ");
            }
            else //全局 机构
            {
                strSql.Append("and UNIT_KEY is null");
            }

            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public JKF.Entities.Model.SYS_PARAM_VALUEinfo GetModel(string PARAM_KEY, string UNIT_KEY, string DEPT_KEY)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,UNIT_KEY,DEPT_KEY,PARAM_KEY,VAL_DATE,VAL,DATE_UPDATE,DATE_CREATE from SYS_PARAM_VALUE ");
            strSql.Append(" where PARAM_KEY=@PARAM_KEY  ");

            if (!string.IsNullOrEmpty(UNIT_KEY))//机构
            {
                strSql.Append("and UNIT_KEY=@UNIT_KEY");
            }
            else//全局
            {
                strSql.Append("and UNIT_KEY is null");
            }
            Database db = DB.HR;
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "@PARAM_KEY", DbType.String, PARAM_KEY);
            db.AddInParameter(dbCommand, "@UNIT_KEY", DbType.String, UNIT_KEY);
            if (!string.IsNullOrEmpty(DEPT_KEY))
            {
                db.AddInParameter(dbCommand, "@DEPT_KEY", DbType.String, DEPT_KEY);
            }
            JKF.Entities.Model.SYS_PARAM_VALUEinfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = partialDal.ReaderBind(dataReader);
                }
            }
            return model;
        }

        public string GetParamValueByKey(string paramKey)
        {
            SYS_PARAMinfo paramInfo = new SYS_PARAMinfo();
            SYS_PARAM_VALUEinfo paramValueInfo = new SYS_PARAM_VALUEinfo();
            paramInfo=dalSysParam.GetModel(paramKey);
            paramValueInfo = GetModel(paramKey, "", "");

            //参数值类型 1 数值 2 时间值 3 bool值 4 其他参数值(表示为字符串形式)
            if (paramInfo.VAL_TYPE == "2")
            {
                if (!string.IsNullOrEmpty(paramValueInfo.VAL_DATE.ToString()))
                    return paramValueInfo.VAL_DATE.ToString();
                else
                    return "";
            }
            else
            {
                if (paramValueInfo != null)
                {
                    if (!string.IsNullOrEmpty(paramValueInfo.VAL.ToString()))
                        return paramValueInfo.VAL.ToString();
                    else
                        return "";
                }
                else
                    return paramInfo.VAL_DEFAULT.ToString().Trim();
            }
        }
    }
}
